<!--
 * @Descripttion: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2021-12-10 10:05:51
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2021-12-10 15:09:28
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在每棵树里找最大值</title>
</head>

<body>
    <script>
        // 您需要在二叉树的每一行中找到最大的值。
        // 层序遍历，找出每一层的最大值
        class Node {
            constructor(root, left, right) {
                this.root = root;
                this.left = left;
                this.right = right;
            }
            largestValues(root) {
                let res, queue = [];
                queue.push(root);
                while (root != null && queue.length) {
                    let max = queue[0].root;
                    let length = queue.length;
                    while (length--) {
                        let node = queue.shift();
                        max = max > node.root ? max : node.root;
                        node.left && queue.push(node.left);
                        node.right && queue.push(node.right);
                    }
                    res.push(max);
                }
                return res;
            }

        }
    </script>

</body>

</html>